package com.acme.bankapp.domain.client;

import java.io.Serializable;

import com.acme.bankapp.domain.account.Account;

/**
 * Class is a presentation of the client.
 *
 * @author S.Zhidkov
 */
public class Client implements Serializable {

    private static final String EMPTY_STRING = "";
    private static final String ERROR_NAME = "Incorrect client name!";
    private static final String ERROR_GENDER = "Incorrect client gender!";

    private String name;
    private Gender gender;
    private Account account;

    /**
     * Creates new client.
     * @param name name of the client
     * @param gender gender of the client
     * @throws IllegalArgumentException if name, age, or gender is incorrect
     */
    public Client(final String name, final Gender gender) {
        if ((name == null) || (EMPTY_STRING.equals(name))) {
            throw new IllegalArgumentException(ERROR_NAME);
        }

        if (gender == null) {
            throw new IllegalArgumentException(ERROR_GENDER);
        }

        this.name = name;
        this.gender = gender;
    }

    /**
     * Returns name of the client.
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * Returns all accounts of the client.
     * @return account of the client
     */
    public Account getAccounts() {
        return account;
    }

    /**
     * Returns client salutation.
     * @return String
     */
    public String getClientSalutation() {
        return gender.getSalutation();
    }

    /**
     * Method adds account to the client.
     * @param account
     */
    public void addAccount(Account account) {
        this.account = account;
    }

    @Override
    public int hashCode() {
        int result = 17;
        result = 37*result + name.hashCode();
        result = 37*result + gender.hashCode();
        result = 37*result + (account == null ? 0 : account.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) { return true; }
        if (!(o instanceof Client)) { return false; }

        final Client c = (Client) o;
        return  (name.equals(c.name))   &&
                (gender == c.gender)    &&
                (account == null ? c.account == null : account.equals(c.account));
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(gender.getSalutation()).append(" ").append(name);
        sb.append(": [gender: ").append(gender);
        sb.append("] ").append(account);
        return sb.toString();
    }
}
